@extends('layouts.adminlayout')

@section('main')
<section class="content">     
    <div class="panel panel-default">
        <div class="panel-heading">Thông tin tài khoản</div>
        <div class='panel-body'>
            <form class="form-horizontal" action="<?php //echo asset('user/infouser'); ?>" method="POST" role="form" name="createuser" id="createuser">
                <fieldset>
                	<div class="form-group">
                        {{ Form::label('fullname', 'Họ tên:',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-6">
                            {{ Form::text('fullname',null,array('class'=>'form-control','required')) }}
                        </div>
                    </div>
                    <div class="form-group">
                        {{ Form::label('username', 'Tài Khoản:',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-6">
                            {{ Form::text('username',null,array('class'=>'form-control','required')) }}
                        </div>
                        <div class="col-lg-3" id="check-username">
                        	
                        </div>
                    </div>

                    <div class="form-group" >
                        {{ Form::label('password', 'Mật Khẩu:',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-6">
                            <input type="password" name="password" id="password" class="form-control" required="required" title="">
                        </div>
                        <div class="col-lg-3" id='check-password'>
                        	
                        </div>
                    </div>
                    <div class="form-group">
                        {{ Form::label('re-password', 'Xác nhận mật khẩu:',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-6">
                        <input type="password" name="re-password" id="re-password" class="form-control" required="required" title="">
                        </div>
                        <div class="col-lg-3" id='check-re-password'>
                        	
                        </div>
                    </div>
                    <div class="form-group">
                        {{ Form::label('gender', 'Giới tinh',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-6">
                            <select name="gender" id="gender" class="form-control" required="required">
                            	<option value="">Chọn</option>
                            	<option value='Nam'>Nam</option>
                            	<option value='Nữ'>Nữ</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        {{ Form::label('email', 'E-mail',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-6">
                            {{ Form::text('email',null,array('class'=>'form-control','required')) }}
                        </div>
                        <div class="col-lg-3" id='check-email'>
                        	
                        </div>
                    </div>
                    <div class="form-group">
                        {{ Form::label('ngay_sinh', 'Ngày sinh',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-6">
                            {{ Form::text('ngay_sinh',null,array('class'=>'form-control','required','id'=>'ngay_sinh')) }}
                        </div>
                        <div class="col-lg-3" id='check-birthday'>
                        	
                        </div>
                    </div>
                    <div class="form-group">
                        {{ Form::label('phone', 'Số điện thoại',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-6">
                            {{ Form::text('phone',null,array('class'=>'form-control','required')) }}
                        </div>
                    </div>
                    <div class="form-group">
                        {{ Form::label('address', 'Địa chỉ',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-6">
                            {{ Form::text('address',null,array('class'=>'form-control','required')) }}
                        </div>
                    </div>
                    <div class="form-group">
                        {{ Form::label('course_id', 'Khóa học',array('class'=>'col-lg-3 control-label')) }}
                        <div class="col-lg-6">
                        <select name="course_id" id="course_id" class="form-control">
                        	<option value="">Chọn</option>
                        	<?php

                        	if(count($courses)){
					            foreach ($courses as $course) {
					                $today = date("Y-m-d");
					                $end_time = str_replace('/', '-', $course->end_time);
					                if(strtotime($today)<=strtotime($end_time)){
					                    echo '<option value="'.$course->id.'">'.$course->course_name.'</option>';
					                }
					            }
					        }
                        	?>
                        </select>
                            
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-lg-3"></div>
                        <div class="col-lg-6">
                        <div id="signin-button" class="show">
                            <button type="submit" class="btn btn-primary">Tạo tài khoản</button>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-lg-12" id="noti"></div>
                    </div>
                </fieldset>
            </form>
        </div>
    </div>
</section><!-- /.content -->
<script type="text/javascript">
	var int_user = 0;
	var int_password = 0;
	var int_re_password = 0;
	var int_email = 0;
	var int_ngaysinh = 0;
	$('#username').change(function(event) {
		if($('#username').val().toString().length < 6){
			$('#check-username').html('<span class="label label-danger">Tài khoản phải lớn hơn 6 kí tự</i></span>');
			int_user = 1;
		}else{
			$.ajax({
			url: '<?php echo asset("ajax/checkusername")?>',
			type: 'post',
			dataType: 'json',
			data: {username: $('#username').val()},
		})
		.done(function(data) {
			if(data == 0){
				$('#check-username').html('<span class="label label-success">Tài khoản khả dụng</i></span>');
				int_user = 0;
			}else{
				$('#check-username').html('<span class="label label-danger">Tài khoản đã tồn tại</i></span>');
				int_user =1;
			}
		})
		.fail(function() {
			console.log("error");
		})
		.always(function() {
			console.log("complete");
		});
		}
		
	});
	$('#password').keyup(function(event) {
		if($('#password').val().toString().length > 6){
			$('#check-password').html('<span class="label label-success">Mật khẩu hợp lệ</i></span>');
			int_password = 0;
		}else{
			$('#check-password').html('<span class="label label-danger">Mật khẩu phải lớn hơn 6 kí tự</i></span>');
			int_password = 1;
		}
	});
	$('#re-password').keyup(function(event) {
		if($('#re-password').val() == $('#password').val()){
			$('#check-re-password').html('<span class="label label-success">Mật khẩu hợp lệ</i></span>');
			int_re_password = 0;
		}else{
			$('#check-re-password').html('<span class="label label-danger">Mật khậu không khớp</i></span>');
			int_re_password =1;
		}
	});
	$('#email').keyup(function(event) {
		/* Act on the event */
		var VAL = $(this).val().toString();

        var email = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);

	   	if(email.test(VAL)){
	   		$('#check-email').html('<span class="label label-success">E-mail hợp lệ</i></span>');
	   		int_email =0;
	   	}else{
	   		$('#check-email').html('<span class="label label-danger">E-mail không hợp lệ</i></span>');
	   		int_email = 1;
	   	}
	});
	$('#ngay_sinh').keyup(function(event) {
		/* Act on the event */
		var val = $(this).val();
		var date = new RegExp(/^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$/);

		if(date.test(val)){
			$('#check-birthday').html('<span class="label label-success">Ngày sinh hợp lệ</i></span>');
			int_ngaysinh = 0;
	   	}else{
	   		$('#check-birthday').html('<span class="label label-danger">Ngày sinh không hợp lệ</i></span>');
	   		int_ngaysinh =1;
	   	}
	});
	$('#createuser').submit(function(event) {
		
		$("#createuser input").prop("disabled", true);
        $("#createuser select").prop("disabled", true);

		$('#noti').html('<br/><div class="alert alert-warning fade in"><p>Đang tạo tài khoản</p></div>');
		if(int_ngaysinh == 0 && int_email == 0 && int_password==0 && int_user == 0 && int_re_password == 0){
			var fullname= $('#fullname').val();
			var username= $('#username').val();
			var password= $('#password').val();
			var email= $('#email').val();
			var birthday= $('#ngay_sinh').val();
			var phone= $('#phone').val();
			var gender = $('#gender').val();
			var address = $('#address').val();
			var course_id = $('#course_id').val();
			$.ajax({
				url: '<?php echo asset("manager-students/createusercourseiframe")?>',
				type: 'post',
				dataType: 'json',
				data: {
					fullname: fullname,
					username: username,
					password: password,
					email: email,
					birthday: birthday,
					phone: phone,
					gender: gender,
					address: address,
					course_id : course_id

				},
			})
			.done(function(data) {
				if(data == 0){
					$('#noti').html('<br/><div class="alert alert-warning fade in"><p>Thất bại. Vui lòng kiểm tra lại thông tin hoặc tải lại trang</p></div>');
				}else{
					$('#noti').html('<br/><div class="alert alert-info fade in"><p>Tạo tài khoản thành công.</p></div>');
					window.history.back()
				}
			})
			.fail(function() {
				$("#createuser input").prop("disabled", false);
        		$("#createuser select").prop("disabled", false);
				$('#noti').html('<br/><div class="alert alert-warning fade in"><p>Thất bại. Vui lòng kiểm tra lại thông tin hoặc tải lại trang</p></div>');
			})
			.always(function() {
				console.log("complete");
			});
		}
		return false;
	});
</script>

@stop